Conditions | 1 |
Paths | 4 |
Total Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Changes | 19 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* |
||
69 | link: function (scope, element, attrs) { |
||
70 | var canvas = element.find('canvas')[0]; |
||
71 | var parent = canvas.parentElement; |
||
72 | var scale = 0; |
||
73 | var ctx = canvas.getContext('2d'); |
||
74 | |||
75 | var width = parseInt(scope.width, 10); |
||
76 | var height = parseInt(scope.height, 10); |
||
77 | |||
78 | canvas.width = width; |
||
79 | canvas.height = height; |
||
80 | |||
81 | scope.signaturePad = new SignaturePad(canvas); |
||
82 | |||
83 | scope.setDataUrl = function(dataUrl) { |
||
84 | var ratio = Math.max(window.devicePixelRatio || 1, 1); |
||
85 | |||
86 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
87 | ctx.scale(ratio, ratio); |
||
88 | |||
89 | scope.signaturePad.clear(); |
||
90 | scope.signaturePad.fromDataURL(dataUrl); |
||
91 | |||
92 | $timeout().then(function() { |
||
93 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
94 | ctx.scale(1 / scale, 1 / scale); |
||
95 | }); |
||
96 | }; |
||
97 | |||
98 | scope.$watch('disabled', function (val) { |
||
99 | val ? scope.signaturePad.off() : scope.signaturePad.on(); |
||
100 | }); |
||
101 | |||
102 | var calculateScale = function() { |
||
103 | var scaleWidth = Math.min(parent.clientWidth / width, 1); |
||
104 | var scaleHeight = Math.min(parent.clientHeight / height, 1); |
||
105 | |||
106 | var newScale = Math.min(scaleWidth, scaleHeight); |
||
107 | |||
108 | if (newScale === scale) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | var newWidth = width * newScale; |
||
113 | var newHeight = height * newScale; |
||
114 | canvas.style.height = Math.round(newHeight) + "px"; |
||
115 | canvas.style.width = Math.round(newWidth) + "px"; |
||
116 | |||
117 | scale = newScale; |
||
118 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
119 | ctx.scale(1 / scale, 1 / scale); |
||
120 | }; |
||
121 | |||
122 | var resizeIH = $interval(calculateScale, 200); |
||
123 | scope.$on('$destroy', function () { |
||
124 | $interval.cancel(resizeIH); |
||
125 | resizeIH = null; |
||
126 | }); |
||
127 | |||
128 | angular.element($window).bind('resize', calculateScale); |
||
129 | scope.$on('$destroy', function () { |
||
130 | angular.element($window).unbind('resize', calculateScale); |
||
131 | }); |
||
132 | |||
133 | calculateScale(); |
||
134 | |||
135 | element.on('touchstart', onTouchstart); |
||
136 | element.on('touchend', onTouchend); |
||
137 | |||
138 | function onTouchstart() { |
||
139 | scope.$apply(function () { |
||
140 | // notify that drawing has started |
||
141 | scope.notifyDrawing({ drawing: true }); |
||
142 | }); |
||
143 | } |
||
144 | |||
145 | function onTouchend() { |
||
146 | scope.$apply(function () { |
||
147 | // updateModel |
||
148 | scope.updateModel(); |
||
149 | |||
150 | // notify that drawing has ended |
||
151 | scope.notifyDrawing({ drawing: false }); |
||
152 | }); |
||
153 | } |
||
154 | } |
||
155 | }; |
||
161 |